﻿#region

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Input;
using Radovici.Ctrls.ContentControls;
using Radovici.Ctrls.Desktop;
using Radovici.Ctrls.Facebook;
using Radovici.SharedLibrary;
using YouTubePlaylist.Models;
using YouTubePlaylist.MusicVideoService;
using YouTubePlaylist.ViewModels;

#endregion

namespace YouTubePlaylist.Views
{
    public partial class Desktop
    {
        private readonly Dictionary<UIElement, Dragable> _dragables = new Dictionary<UIElement, Dragable>();

        public Desktop(DesktopViewModel vm)
        {
            InitializeComponent();
            DataContext = vm;
            DragableCanvas.SizeChanged += delegate(object sender, SizeChangedEventArgs sizeEventArgs)
                                              {
                                                  MainGrid.Width = sizeEventArgs.NewSize.Width;
                                                  MainGrid.Height = sizeEventArgs.NewSize.Height;
                                              };
        }

        private DesktopViewModel ViewModel
        {
            get { return DataContext as DesktopViewModel; }
        }

        private void OnWidgetLoaded(object sender, RoutedEventArgs e)
        {
            var ctrl = sender as ContentControl;
            if (ctrl != null)
            {
                var dragCtrl = ctrl.FindParent<Dragable>();
                if (dragCtrl != null)
                {
                    _dragables[ctrl] = dragCtrl;
                    dragCtrl.Popout();
                }
                var model = ctrl.DataContext as WidgetModel;
                if (model != null)
                {
                    ctrl.Content = model.LoadView(); //NOTE: This affect OnApplyTemplate override
                }
            }
        }

        private void OnWidgetClosed(object sender, CancelEventArgs cancel)
        {
            var ctrl = sender as ContentControl;
            if (ctrl != null)
            {
                var disposable = ctrl.Content as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }

                if (_dragables.ContainsKey(ctrl))
                {
                    _dragables[ctrl].Popin();
                    _dragables.Remove(ctrl);
                }

                var model = ctrl.DataContext as WidgetModel;
                if (model != null)
                {
                    DesktopViewModel desktop = ViewModel;
                    if (desktop != null)
                    {
                        desktop.Model.Remove(model);
                    }
                }
            }
        }

        private void OnMenuItemClicked(object sender, RoutedEventArgs e)
        {
            var template = ((FrameworkElement) sender).DataContext as MenuItemModel;
            if (template != null)
            {
                ViewModel.Model.Add(template.CreateWidgetModel());
            }
        }

        //#region FacebookIntegration
        //private void OnFacebookUserClicked(object sender, RoutedEventArgs e)
        //{
        //    var btn = sender as Button;
        //    if (btn != null)
        //    {
        //        OpenUserProfile(btn.DataContext as LogMessage);
        //    }
        //}

        //private void OnFacebookUserNameClicked(object sender, MouseButtonEventArgs e)
        //{
        //    var btn = sender as TextBlock;
        //    if (btn != null)
        //    {
        //        OpenUserProfile(btn.DataContext as LogMessage);
        //    }
        //}

        //private void OpenUserProfile(LogMessage msg)
        //{
        //    if (msg != null)
        //    {
        //        if (HtmlPage.IsPopupWindowAllowed)
        //        {
        //            FacebookApi.FacebookUser fbUser = msg.FacebookUser;
        //            if (fbUser != null && !string.IsNullOrEmpty(fbUser.Profile))
        //            {
        //                HtmlPage.PopupWindow(new Uri(fbUser.Profile), string.Empty, new HtmlPopupWindowOptions());
        //            }
        //        }
        //    }
        //}

        //private void OnPublishMessage(object sender, RoutedEventArgs e)
        //{
        //    var btn = sender as Button;
        //    if (btn != null)
        //    {
        //        var msg = btn.DataContext as LogMessage;
        //        if (msg != null)
        //        {
        //            DataLayer.Singleton.Publish(msg);
        //        }
        //    }
        //}
        //#endregion
    }
}